﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Hired
{
    public partial class HiredParent : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                string userID = Page.User.Identity.Name;

                if (Session["username"] == null)
                {
                    if (Page.User.IsInRole("admin"))
                    {
                        Session["username"] = DBAdminStaffAccount.retrieveAdminStaffAccountByID(userID).Username;
                    }
                    else if (Page.User.IsInRole("company"))
                    {
                        Session["username"] = DBCompanyAccount.RetrieveCompanyAccount(userID, false).Username;
                    }
                    else if (Page.User.IsInRole("employer"))
                    {
                        Session["username"] = DBEmployerPassport.RetrieveEmployerPassport(userID).Username;
                    }
                    else if (Page.User.IsInRole("jobseeker"))
                    {
                        Session["username"] = DBJobseekerAccount.retrieveJobseekerAccount(userID, false).Username;
                    }
                }

                lblUsername.Text = (string)(Session["username"]);
                // other if-else codes to check account type and change divs accordingly
                if (Page.User.IsInRole("admin"))
                {
                    pnlNavLogin.CssClass = "nav_admin";
                    lblLogin.Visible = false;
                    lblAdmin.Visible = true;
                }
                else
                {
                    pnlNavEmployers.Visible = false;
                    pnlNavLogin.CssClass = "nav_profile";
                    lblLogin.Visible = false;
                    lblProfile.Visible = true;
                }
            }
            else
            {
                pnlAccount.Visible = false;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text != "" && txtPassword.Text != "")
            {
                JobseekerAccount accountToLogin = new JobseekerAccount();
                if (DBJobseekerAccount.retrieveJobseekerAccountbyUsername(txtUsername.Text, false) != null)
                {
                    accountToLogin = DBJobseekerAccount.retrieveJobseekerAccountbyUsername(txtUsername.Text, false);
                }
                if (Encryption.verify(txtPassword.Text, accountToLogin.Password))
                {
                    if ((bool)accountToLogin.Activated)
                    {
                        Session["username"] = accountToLogin.Username;
                        FormsAuthentication.RedirectFromLoginPage(accountToLogin.JAccountID, true);
                        Response.Redirect(Request.Url.AbsolutePath);
                    }
                    else
                    {
                        Session["usernameToActivate"] = accountToLogin.Username;
                        Response.Redirect("notactivated.aspx", false);
                    }
                }
                else
                {
                    Response.Redirect("jslogin.aspx?error=0");
                }
            }
            else 
            {
                Response.Redirect("jslogin.aspx?error=1");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //txtSearch.Text = "Search is not working at the moment. :(";
            if (txtSearch.Text != "")
            {
                Response.Redirect("results.aspx?search=" + txtSearch.Text);
            }
        }

        protected void lblLogout_Click(object sender, EventArgs e)
        {
            // logout code here
            FormsAuthentication.SignOut();
            Response.Redirect("home.aspx");
        }
    }
}